package com.company.project.web;
import com.alibaba.fastjson.JSON;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.News;
import com.company.project.model.NewsDetail;
import com.company.project.service.impl.NewsServiceImpl;
import com.company.project.util.FileUploadUtil;
import com.company.project.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
* Created by CodeGenerator on 2018/10/08.
*/
@RestController
@RequestMapping("/news")
public class NewsController {
    @Resource
    @Autowired
    private NewsServiceImpl newsService;

    @Resource
    private FileUploadUtil fileUploadUtil;

    /**
     * 根据活动查询
     * @param
     * @return
     */
    @RequestMapping(value = "/selectNews/{title}",method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public List selectNews(@PathVariable("title") String title) {
        return newsService.selectNews(title);
    }
    /**
     * 后台查询所有新闻
     */
    @GetMapping("/selectAll/{deployModule}/{index}")
    @ResponseBody
    public Result selectNewsAll(@PathVariable("deployModule")String deployModule, @PathVariable(value = "index") Integer index) {
        PageUtil list = newsService.selectNewsAll(deployModule,index);
        return ResultGenerator.genSuccessResult(list);
    }

    /**
     * 添加工作动态
     * @param news
     * @return
     */
    @PostMapping("/addWorkTrend")
    @ResponseBody
    public Result addWorkTrend(@RequestParam String news,@RequestParam(required = false) MultipartFile file){
        News news1 = JSON.parseObject(news,News.class);
        if (file!=null){
            String s = fileUploadUtil.writeFile(file, "img");
            if (s == null){
                return ResultGenerator.getInternalServerErrot("图片上传错误");
            }
            news1.setPhoneUri(s);
            Boolean aBoolean = newsService.addWorkTrend(news1);
            if (aBoolean.equals(true)){
                return ResultGenerator.genSuccessResult(aBoolean);
            }
        }
        Boolean aBoolean = newsService.addWorkTrend(news1);
        if (aBoolean.equals(true)){
            return ResultGenerator.genSuccessResult(aBoolean);
        }
        return ResultGenerator.getInternalServerErrot("传输错误");
    }
    /**
     * 添加上架
     */
    @PostMapping("/addPutAway")
    @ResponseBody
    public Result addPutAway(@RequestParam String news,@RequestParam(required = false) MultipartFile file){
        News news1 = JSON.parseObject(news,News.class);
        if (file!=null){
            String s = fileUploadUtil.writeFile(file, "img");
            if (s == null){
                return ResultGenerator.getInternalServerErrot("图片上传错误");
            }
            news1.setPhoneUri(s);
            Boolean aBoolean = newsService.addPutAway(news1);
            if (aBoolean.equals(true)){
                return ResultGenerator.genSuccessResult(aBoolean);
            }
        }
        Boolean aBoolean = newsService.addPutAway(news1);
        if (aBoolean.equals(true)){
            return ResultGenerator.genSuccessResult(aBoolean);
        }
        return ResultGenerator.genSuccessResult("传输错误");
    }
    /**
     * 直接上传
     * @param news
     * @return
     */
    @PostMapping("/updateNews")
    @ResponseBody
    public Result updateNews(@RequestParam String news,@RequestParam(required = false) MultipartFile file){
        News news1 = JSON.parseObject(news,News.class);
        if (file!=null){
            String s = fileUploadUtil.writeFile(file, "img");
            if (s == null){
                return ResultGenerator.getInternalServerErrot("图片上传错误");
            }
            news1.setPhoneUri(s);
            Boolean aBoolean = newsService.updateNews(news1);
            if (aBoolean.equals(true)){
                return ResultGenerator.genSuccessResult(aBoolean);
            }
        }
        Boolean aBoolean = newsService.updateNews(news1);
        if (aBoolean.equals(true)){
            return ResultGenerator.genSuccessResult(aBoolean);
        }
        return ResultGenerator.genSuccessResult("传输错误");
    }
    /**
     * 删除工作动态
     * @param id
     * @return
     */
    @GetMapping("/delWorkTrend/{id}")
    @ResponseBody
    public Result delWorkTrend(@PathVariable("id") Integer id){
        News news = new News();
        news.setId(id);
        NewsDetail newsDetail = new NewsDetail();
        newsDetail.setNewsId(id);
        news.setNewsDetail(newsDetail);
        Boolean aBoolean = newsService.delWorkTrend(news);
        return ResultGenerator.genSuccessResult(aBoolean);
    }

    /**
     * 修改工作动态
     * @param news
     * @return
     */
    @PostMapping("/updateWorkTrend")
    @ResponseBody
    public Result updateWorkTrend(@RequestParam String news,@RequestParam(required = false) MultipartFile file){
        News news1 = JSON.parseObject(news,News.class);
        if (file!=null){
            String s = fileUploadUtil.writeFile(file, "img");
            if (s == null){
                return ResultGenerator.getInternalServerErrot("图片上传错误");
            }
            news1.setPhoneUri(s);
            Boolean aBoolean = newsService.updateWorkTrend(news1);
            if (aBoolean.equals(true)){
                return ResultGenerator.genSuccessResult(aBoolean);
            }
        }
        Boolean aBoolean = newsService.updateWorkTrend(news1);
        if (aBoolean.equals(true)){
            return ResultGenerator.genSuccessResult(aBoolean);
        }
        return ResultGenerator.genSuccessResult("传输错误");
    }

    @PostMapping("/add")
    public Result add(News news) {
        newsService.save(news);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        newsService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(News news) {
        newsService.update(news);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        News news = newsService.findById(id);
        return ResultGenerator.genSuccessResult(news);
    }

    //获取新闻
    @GetMapping("/list")
    @ResponseBody
    public Result list() {
        Map<String, List<News>> list = newsService.getNewsByDeployModule();
        return ResultGenerator.genSuccessResult(list);
    }

    //获取全部新闻
    @GetMapping("/list/all/{deployModule}/{index}")
    @ResponseBody
    public Result listAll(@PathVariable("deployModule")String deployModule, @PathVariable(value = "index") Integer index) {
        PageUtil list = newsService.getNewsAllByDeployModule(deployModule,index);
        return ResultGenerator.genSuccessResult(list);
    }

    //获取新闻详情
    @GetMapping("/new/{id}")
    @ResponseBody
    public Result listAll(@PathVariable("id")Integer id) {
        News news = newsService.getNewsById(id);
        return ResultGenerator.genSuccessResult(news);
    }

    /**
     * 前台搜索框按照活动查询
     * @param cname
     * @param index
     * @return
     */
    @PostMapping("/queryNews/{index}")
    @ResponseBody
    public PageUtil queryNews(@RequestBody String cname ,@PathVariable("index") Integer index){
        PageUtil<Map<Object,Object>> newsPageUtil = newsService.queryNews(cname, index);
        return newsPageUtil;
    }
}
